package com.app.micaihu.bean;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.app.micaihu.R;
import com.app.micaihu.configure.ActivityRequestCode;
import com.app.micaihu.configure.AppConfigTools;
import com.app.micaihu.utils.OkHttp3Helper;
import com.app.utils.util.ToastUtils;
import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.AppUtils;

import java.io.File;

/**
 * 应用app
 *
 * @author linkai
 */
public class App {

    private String name;// 应用名称
    private String describe;// 应用描述
    private String logo;// 应用logo的下载地址
    private String app_url;// app的下载地址
    private Context mContext;

    public App() {

    }

    public String getDescribe() {
        return describe;
    }


    public void setDescribe(String describe) {
        this.describe = describe;
    }


    public String getLogo() {
        return logo;
    }


    public void setLogo(String logo) {
        this.logo = logo;
    }


    public String getApp_url() {
        return app_url;
    }


    public void setApp_url(String app_url) {
        this.app_url = app_url;
    }

    public App(Context c) {
        this.mContext = c;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public Context getmContext() {
        return mContext;
    }

    public void setmContext(Context mContext) {
        this.mContext = mContext;
    }

    /**
     * 下载app
     */
    public void startDownApp() {
        if (this.app_url == null || this.app_url.equals("")) {
            Builder alertDialog = new Builder(mContext);
            alertDialog.setTitle("提示");
            alertDialog.setMessage("暂时无法获取下载连接,请稍后再试");
            alertDialog.setPositiveButton(
                    mContext.getString(R.string.common_confirm),
                    new DialogInterface.OnClickListener() {

                        @Override
                        public void onClick(DialogInterface dialog,
                                            int which) {
                            dialog.dismiss();
                            dialog.cancel();
                        }

                    }).show();
            return;
        }

        String target = AppConfigTools.APKFLODER;
        File f = new File(target);
        if (!f.exists()) {
            f.mkdirs();
        }
        target = AppConfigTools.APKFLODER + AppConfigTools.SPREARATOR + app_url.substring(app_url.lastIndexOf("/") + 1);
        f = new File(target);
        if (f.exists()) {
            f.delete();
        }

        final Handler handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
                    if (TextUtils.isEmpty(msg.obj.toString())) {
                        ToastUtils.showText("下载失败,请重新下载");
                        return;
                    }
//					File file = new File(msg.obj.toString());
                    try {
                        File file = (File) msg.obj;
                        //跳转安装文件
                        installFile(file, mContext);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } else {
                    ToastUtils.showText("下载失败,请稍后重试");
                }

            }
        };
        ToastUtils.makeText("开始下载,请稍后");

        int separatorIndex = app_url.lastIndexOf("/");
        String fileName = (separatorIndex < 0) ? app_url : app_url.substring(separatorIndex + 1, app_url.length());
        OkHttp3Helper.getInstance().downAsynFile(app_url, AppConfigTools.APKFLODER, fileName, new OkHttp3Helper.OnDownLoadResponCallBack() {
            @Override
            public void onStart() {
                super.onStart();
            }

            @Override
            public void onFailure() {
                super.onFailure();
                if (handler != null) {
                    handler.sendEmptyMessage(0);
                }
            }

            @Override
            public void onSucess(File file) {
                super.onSucess(file);
                if (handler != null) {
                    Message message = new Message();
                    message.obj = file;
                    message.what = 1;
                    handler.sendMessage(message);
                }

            }
        });

    }

    /**
     * 安装文件
     *
     * @param file
     * @param context
     */
    private void installFile(File file, Context context) {
        if (file == null) {
            ToastUtils.makeText("下载失败,请重新下载");
            return;
        }
        try {
            AppUtils.installApp(file);
        } catch (Exception e) {
            ToastUtils.makeText("安装错误");
        }
    }

}
